package com.atguigu.educms.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 后台管理系统
 * @author 董万均
 * @since 2020-11-01
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("pageBanner/{current}/{limit}")//current是当前页，limit是每页中的条数
    public Result pageBanner(@PathVariable("current") long current,@PathVariable("limit") long limit){
        Page<CrmBanner> page=new Page<>(current,limit);
        bannerService.page(page,null);
        return Result.ok().data("items",page.getRecords()).data("total",page.getTotal());
    }

    //增加banner
    @ApiOperation(value = "增加轮播图")
    @PostMapping("addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return Result.ok();
    }

    //查询Banner
    @ApiOperation(value = "查询轮播图")
    @GetMapping("getBanner/{id}")
    public Result getBanner(@PathVariable String id){
        CrmBanner banner = bannerService.getById(id);
        return Result.ok().data("item",banner);
    }

    //修改banner
    @ApiOperation(value = "修改轮播图")
    @PostMapping("updateBanner")
    public Result updateBanner(@RequestBody CrmBanner crmBanner){
        bannerService.updateById(crmBanner);
        return Result.ok();
    }

    //删除banner
    @ApiOperation(value ="删除轮播图")
    @DeleteMapping("deleteBanner/{id}")
    public Result deleteBanner(@PathVariable String id){
        bannerService.removeById(id);
        return Result.ok();
    }
}

